import numpy as np

# 第二章 感知机


def AND(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.5
    tmp = x1 * w1 + x2 * w2
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1


print("AND:")
print(AND(0, 0))
print(AND(1, 0))
print(AND(0, 1))
print(AND(1, 1))


def NAND(x1, x2):
    w1, w2, theta = -0.5, -0.5, 0.7
    tmp = x1 * w1 + x2 * w2
    if tmp + theta <= 0:
        return 0
    else:
        return 1


def NAND_NP(x1, x2):
    w1, w2, theta = -0.5, -0.5, 0.7
    x = np.array([x1, x2]);
    w = np.array([w1, w2])
    tmp = np.sum(x * w) + theta
    if tmp <= 0:
        return 0
    else:
        return 1


print("NAND:")
print(NAND_NP(0, 0))
print(NAND_NP(1, 0))
print(NAND_NP(0, 1))
print(NAND_NP(1, 1))


def OR(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.2
    tmp = x1 * w1 + x2 * w2
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1


print("OR:")
print(OR(0, 0))
print(OR(1, 0))
print(OR(0, 1))
print(OR(1, 1))


def XOR(x1, x2):
    t2 = NAND(x1, x2)
    t1 = OR(x1, x2)
    tmp = AND(t2, t1)
    return tmp


print("XOR:")
print(XOR(0, 0))
print(XOR(1, 0))
print(XOR(0, 1))
print(XOR(1, 1))
